﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Xml;
using WebService.BUS;
using WebService.VO;
using System.Text;
using System.Drawing;
using WebService.DAO;

namespace WebService
{
    /// <summary>
    /// Summary description for GPSTravelServices
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class GPSTravelServices : System.Web.Services.WebService
    {

        [WebMethod]
        public String getNewDataWithDistance(string latitude, string longitude, string scope, string language)
        {
            try
            {
                // create new DataTable
                DataTable dtt = new DataTable("Items");
                // create new DataSet
                DataSet ds = new DataSet("GPSTravel");
                // add DataTable to DataSet
                ItemBUS itemBUS = new ItemBUS();
                LanguageBUS lang= new LanguageBUS();
                if (!lang.check(language))
                {
                    language = "vi";
                }
                //remove when support multilang places info
                language = "vi";
                dtt = itemBUS.getItemsList(Convert.ToDouble(latitude), Convert.ToDouble(longitude), Convert.ToDouble(scope), language);
                if (dtt != null)
                {
                    dtt.TableName = "Items";
                    ds.Tables.Add(dtt);
                    // Create Xml data from DataSet data
                    string strXdoc = ds.GetXml();
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(strXdoc);
                    // Return Xml Document
                    StringWriter sw = new StringWriter();
                    XmlTextWriter xw = new XmlTextWriter(sw);
                    xdoc.WriteTo(xw);
                    return sw.ToString();
                }
                return "Error: No data";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        /// <summary>
        /// Lay danh sach cac dia diem theo loai
        /// </summary>
        /// <param name="latitude"></param>
        /// <param name="longtitude"></param>
        /// <param name="scope"></param>
        /// <param name="CategoryId"></param>
        /// <returns></returns>

        [WebMethod]
        public String getNewDataWithIdAndDistance(string latitude, string longitude, string scope, string CategoryId, string strDeviceID, string language)
        {
            try
            {
                LanguageBUS lang = new LanguageBUS();
                if (!lang.check(language))
                {
                    language = "vi";
                }
                //remove when support multilang places info
                language = "vi";
                DeviceBUS itemDeviceBUS = new DeviceBUS();
                DeviceVO item = new DeviceVO();
                item = itemDeviceBUS.getDevice(strDeviceID);
                DeviceHistoryVO deviceHistory = new DeviceHistoryVO();
                deviceHistory.Device = item;
                deviceHistory.Date = DateTime.Now;
                deviceHistory.X = Convert.ToDouble(latitude);
                deviceHistory.Y = Convert.ToDouble(longitude);
                itemDeviceBUS.insertHistory(deviceHistory);
                // create new DataTable
                DataTable dtt = new DataTable("Items");
                // create new DataSet
                DataSet ds = new DataSet("GPSTravel");
                // add DataTable to DataSet
                ItemBUS itemBUS = new ItemBUS();
                dtt = itemBUS.getItemsList(Convert.ToDouble(latitude), Convert.ToDouble(longitude), Convert.ToDouble(scope), Convert.ToInt32(CategoryId), strDeviceID, language);
                if (dtt != null)
                {
                    dtt.TableName = "Items";
                    ds.Tables.Add(dtt);
                    // Create Xml data from DataSet data
                    string strXdoc = ds.GetXml();
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(strXdoc);
                    // Return Xml Document
                    StringWriter sw = new StringWriter();
                    XmlTextWriter xw = new XmlTextWriter(sw);
                    xdoc.WriteTo(xw);
                    return sw.ToString();
                }
                return "Error: No data";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }


        /// <summary>
        /// lay danh sach cac dia diem
        /// </summary>
        /// <param name="latitude"></param>
        /// <param name="longtitude"></param>
        /// <param name="strDeviceID"></param>
        /// <returns></returns>


        [WebMethod]
        public String getNewData(string latitude, string longitude, string strDeviceID, string language)
        {
            try
            {
                LanguageBUS lang = new LanguageBUS();
                if (!lang.check(language))
                {
                    language = "vi";
                }
                //remove when support multilang places info
                language = "vi";
                // create new DataTable
                DataTable dtt = new DataTable("Items");
                // create new DataSet
                DataSet ds = new DataSet("GPSTravel");
                // add DataTable to DataSet
                ItemBUS itemBUS = new ItemBUS();
                dtt = itemBUS.getItemsList(Convert.ToDouble(latitude), Convert.ToDouble(longitude), 10, language);
                if (dtt != null)
                {
                    dtt.TableName = "Items";
                    ds.Tables.Add(dtt);
                    // Create Xml data from DataSet data
                    string strXdoc = ds.GetXml();
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(strXdoc);
                    // Return Xml Document
                    StringWriter sw = new StringWriter();
                    XmlTextWriter xw = new XmlTextWriter(sw);
                    xdoc.WriteTo(xw);
                    return sw.ToString();
                }
                return "Error: No data";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }

        /// <summary>
        /// lay danh sach cac dia diem
        /// </summary>
        /// <param name="latitude"></param>
        /// <param name="longtitude"></param>
        /// <param name="strDeviceID"></param>
        /// <returns></returns>


        [WebMethod]
        public String getDataByDirections(string start, string end, string language, string id)
        {
            try
            {
                LanguageBUS lang = new LanguageBUS();
                if (!lang.check(language))
                {
                    language = "vi";
                }
                //remove when support multilang places info
                language = "vi";
                // create new DataTable
                DataTable dtt = new DataTable("Items");
                ItemDAO itemDAO = new ItemDAO();
                if (id.Equals("0"))
                {
                    dtt = itemDAO.getItemsList(language);
                }
                else 
                {
                    dtt = itemDAO.getItemsList(Convert.ToInt32(id),language);
                }
                dtt.Columns.Add("Distance", typeof(Double));
                // create new DataSet
                DataSet ds = new DataSet("GPSTravel");
                // add DataTable to DataSet
                ItemBUS itemBUS = new ItemBUS();
                RouteVO route = GDirections.parse(start, end);
                DataTable dt = GooglePlaces.getPlaces(dtt, route);
                Utility.RemoveDuplicateRows(dt, "ItemID");
                if (dt != null)
                {
                    dt.TableName = "Items";
                    ds.Tables.Add(dt);
                    // Create Xml data from DataSet data
                    string strXdoc = ds.GetXml();
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(strXdoc);
                    // Return Xml Document
                    StringWriter sw = new StringWriter();
                    XmlTextWriter xw = new XmlTextWriter(sw);
                    xdoc.WriteTo(xw);
                    return sw.ToString();
                }
                return "Error: No data";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }

        

        /// <summary>
        /// Search Item
        /// </summary>
        /// <param name="latitude"></param>
        /// <param name="longtitude"></param>
        /// <param name="strDeviceID"></param>
        /// <returns></returns>

        [WebMethod]
        public String searchItem(string keyword, string latitude, string longitude, string language)
        {
            try
            {
                LanguageBUS lang = new LanguageBUS();
                if (!lang.check(language))
                {
                    language = "vi";
                }
                //remove when support multilang places info
                language = "vi";
                // create new DataTable
                DataTable dtt = new DataTable("Items");
                // create new DataSet
                DataSet ds = new DataSet("GPSTravel");
                // add DataTable to DataSet
                ItemBUS itemBUS = new ItemBUS();
                dtt = itemBUS.search(keyword, Convert.ToDouble(latitude), Convert.ToDouble(longitude), language);
                if (dtt != null)
                {
                    dtt.TableName = "Items";
                    ds.Tables.Add(dtt);
                    // Create Xml data from DataSet data
                    string strXdoc = ds.GetXml();
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(strXdoc);
                    // Return Xml Document
                    StringWriter sw = new StringWriter();
                    XmlTextWriter xw = new XmlTextWriter(sw);
                    xdoc.WriteTo(xw);
                    return sw.ToString();
                }
                return "Error: No data";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }

        /// <summary>
        /// get Category list
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public String getCategoryList(string language)
        {
            try
            {
                LanguageBUS lang = new LanguageBUS();
                if (!lang.check(language.Trim()))
                {
                    language = "en";
                }
                // create new DataTable
                DataTable dtt = new DataTable("Category");
                // create new DataSet
                DataSet ds = new DataSet("GPSTravel");
                // add DataTable to DataSet
                CategoryBUS cateBUS = new CategoryBUS();
                dtt = cateBUS.getCategoryList(language.Trim());
                if (dtt != null)
                {
                    dtt.TableName = "Category";
                    ds.Tables.Add(dtt);
                    // Create Xml data from DataSet data
                    string strXdoc = ds.GetXml();
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(strXdoc);
                    // Return Xml Document
                    StringWriter sw = new StringWriter();
                    XmlTextWriter xw = new XmlTextWriter(sw);
                    xdoc.WriteTo(xw);
                    return sw.ToString();
                }
                return "Error: No data";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }
        /// <summary>
        /// Get comments by Item
        /// </summary>
        /// <param name="ItemID"></param>
        /// <returns></returns>
        [WebMethod]
        public String getComments(string ItemID)
        {
            try
            {
                // create new DataTable
                DataTable dtt = new DataTable("UserComment");
                // create new DataSet
                DataSet ds = new DataSet("GPSTravel");
                // add DataTable to DataSet
                UserCommentBUS cmtBUS = new UserCommentBUS();
                dtt = cmtBUS.getCommentByItem(Convert.ToInt16(ItemID));
                if (dtt != null)
                {
                    dtt.TableName = "UserComment";
                    ds.Tables.Add(dtt);
                    // Create Xml data from DataSet data
                    string strXdoc = ds.GetXml();
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(strXdoc);
                    // Return Xml Document
                    StringWriter sw = new StringWriter();
                    XmlTextWriter xw = new XmlTextWriter(sw);
                    xdoc.WriteTo(xw);
                    return sw.ToString();
                }
                return "Error: No data";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }
        /// <summary>
        /// update Rating
        /// </summary>
        /// <param name="ItemID"></param>
        /// <param name="strDeviceItems"></param>
        /// <param name="rateClient"></param>
        /// <returns></returns>
        [WebMethod]
        public String updateRating(string ItemID, string strDeviceItems, string rateClient)
        {
            try
            {
                UserRatingBUS userRatingBUS = new UserRatingBUS();
                UserRatingVO userRating = new UserRatingVO();
                ItemBUS itemBUS = new ItemBUS();
                ItemVO item = new ItemVO();
                item = itemBUS.getItems(ItemID,"vi");
                DeviceBUS deviceBUS = new DeviceBUS();
                DeviceVO device = deviceBUS.getDevice(strDeviceItems);
                userRating.Item = item;
                userRating.Device = device;
                userRating.Rating = Convert.ToDouble(rateClient);
                userRatingBUS.updateRating(userRating);
                return "Update rating success";
            }
            catch (Exception ex)
            {
                // throw exception
                return ex.Message;
            }
        }
        /// <summary>
        /// insert user comment
        /// </summary>
        /// <param name="ItemID"></param>
        /// <param name="strDeviceItems"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        [WebMethod]
        public String insertComment(string ItemID, string strDeviceItems, string comment)
        {
            try
            {
                UserCommentVO usercomment = new UserCommentVO();
                UserCommentBUS commentBUS = new UserCommentBUS();
                ItemBUS itemBUS = new ItemBUS();
                ItemVO item = new ItemVO();
                item = itemBUS.getItems(ItemID,"vi");
                DeviceBUS deviceBUS = new DeviceBUS();
                DeviceVO device = deviceBUS.getDevice(strDeviceItems);
                usercomment.Item = item;
                usercomment.Device = device;
                usercomment.Comment = comment;
                usercomment.Time = DateTime.Now;
                commentBUS.insertComment(usercomment);
                return "Insert cooment success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// insert item into user favorite list 
        /// </summary>
        /// <param name="ItemID"></param>
        /// <param name="strDeviceItems"></param>
        /// <returns></returns>
        [WebMethod]
        public String insertFavorite(string ItemID, string strDeviceItems)
        {
            try
            {
                FavoriteVO favorite = new FavoriteVO();
                FavoriteBUS favoriteBUS = new FavoriteBUS();
                ItemBUS itemBUS = new ItemBUS();
                ItemVO item = new ItemVO();
                item = itemBUS.getItems(ItemID, "vi");
                DeviceBUS deviceBUS = new DeviceBUS();
                DeviceVO device = deviceBUS.getDevice(strDeviceItems);
                favorite.Item = item;
                favorite.Device = device;
                favoriteBUS.insert(favorite);
                return "Insert favorite success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// Get favorite by user device
        /// </summary>
        /// <param name="ItemID"></param>
        /// <returns></returns>
        [WebMethod]
        public String getFavorite(string strDeviceItems, string language)
        {
            try
            {
                LanguageBUS lang = new LanguageBUS();
                if (!lang.check(language))
                {
                    language = "vi";
                }
                // create new DataTable
                DataTable dtt = new DataTable("Items");
                // create new DataSet
                DataSet ds = new DataSet("GPSTravel");
                DeviceBUS deviceBUS = new DeviceBUS();
                DeviceVO device = deviceBUS.getDevice(strDeviceItems);
                // add DataTable to DataSet
                FavoriteBUS fvrBUS = new FavoriteBUS();
                dtt = fvrBUS.getFavoriteList(device, language);
                if (dtt.Rows[0] != null)
                {
                    dtt.TableName = "Items";
                    ds.Tables.Add(dtt);
                    // Create Xml data from DataSet data
                    string strXdoc = ds.GetXml();
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(strXdoc);
                    // Return Xml Document
                    StringWriter sw = new StringWriter();
                    XmlTextWriter xw = new XmlTextWriter(sw);
                    xdoc.WriteTo(xw);
                    return sw.ToString();
                }
                return "Error: No data";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }

        /// <summary>
        /// insert itemdevice
        /// </summary>
        /// <param name="ItemDevice"></param>
        /// <param name="Email"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <returns></returns>
        [WebMethod]
        public String insertDevice(string ItemDevice, string UserName, string Email, string X, string Y)
        {
            try
            {
                DeviceBUS itemDeviceBUS = new DeviceBUS();
                DeviceVO item = new DeviceVO();
                item.DeviceID = ItemDevice.Trim() ;
                item.UserName = UserName;
                item.Email = Email;
                item.RegisterDate = DateTime.Now;
                bool kq = itemDeviceBUS.insert(item);
                DeviceVO device_temp = itemDeviceBUS.getDevice(ItemDevice.Trim());
                DeviceHistoryVO deviceHistory = new DeviceHistoryVO();
                deviceHistory.Device = device_temp;
                deviceHistory.Date = DateTime.Now;
                deviceHistory.X = Convert.ToDouble(X);
                deviceHistory.Y = Convert.ToDouble(Y);
                itemDeviceBUS.insertHistory(deviceHistory);
                return "Success";
            }
            catch (Exception ex)
            {
                // throw exception
                return ex.Message;
            }
        }
        /// <summary>
        /// Add new place
        /// </summary>
        /// <param name="ItemName"></param>
        /// <param name="CategoryID"></param>
        /// <param name="Address"></param>
        /// <param name="Phone"></param>
        /// <param name="Email"></param>
        /// <param name="Note"></param>
        /// <param name="UrlImage"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="strDeviceItems"></param>
        /// <returns></returns>

        [WebMethod]
        public String addNewData(string ItemName, string CategoryID, string Address, string Phone, string Email, string Note, string UrlImage, string X, string Y, string strDeviceItems)
        {
            try
            {
                ItemBUS itemBUS = new ItemBUS();
                ItemVO item = new ItemVO();
                item.ItemId = 0;
                item.ItemName = ItemName;
                item.Rate = 0;
                item.Address = Address;
                item.Phone = Phone;
                item.Email = Email;
                CategoryVO cate = new CategoryVO();
                cate.CategoryId = Convert.ToInt32(CategoryID);
                item.Category = cate;
                item.Note = Note;
                item.X = Convert.ToDouble(X);
                item.Y = Convert.ToDouble(Y);
                item.CountRate = 0;
                item.Image = UrlImage;
                item.Check = 0;
                itemBUS.insert(item);
                DeviceBUS deviceBUS = new DeviceBUS();
                DeviceVO device = deviceBUS.getDevice(strDeviceItems);
                ItemVO item_temp = itemBUS.getItem(item.ItemName,item.X,item.Y);
                UserPlaceVO place = new UserPlaceVO();
                place.Device = device;
                place.Item = item_temp;
                UserPlacesBUS placeBUS = new UserPlacesBUS();
                placeBUS.insert(place);
                return "Add new place success";
            }
            catch (Exception ex)
            {
                // throw exception
                return ex.Message;
            }
        }

        [WebMethod]
        public String getIDDevice(string strDeviceItems)
        {
            DeviceBUS deviceBUS = new DeviceBUS();
            DeviceVO device = deviceBUS.getDevice(strDeviceItems);
            return device.Id.ToString();
        }

        /// <summary>
        /// update place info
        /// </summary>
        /// <param name="ItemID"></param>
        /// <param name="ItemName"></param>
        /// <param name="CategoryID"></param>
        /// <param name="Address"></param>
        /// <param name="Phone"></param>
        /// <param name="Email"></param>
        /// <param name="Note"></param>
        /// <param name="UrlImage"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="strDeviceItems"></param>
        /// <returns></returns>
        [WebMethod]
        public String updateNewData(string ItemID, string ItemName, string CategoryID, string Address, string Phone, string Email, string Note, string UrlImage, string X, string Y, string strDeviceItems)
        {
            try
            {
                ItemBUS itemBUS = new ItemBUS();
                ItemVO item = new ItemVO();
                item.ItemId = Convert.ToInt32(ItemID.Trim()) ;
                item.ItemName = ItemName;
                item.Rate = 0;
                item.Address = Address;
                item.Phone = Phone;
                item.Email = Email;
                CategoryVO cate = new CategoryVO();
                cate.CategoryId = Convert.ToInt32(CategoryID.Trim());
                item.Category = cate;
                item.Note = Note;
                item.X = Convert.ToDouble(X.Trim());
                item.Y = Convert.ToDouble(Y.Trim());
                item.CountRate = 0;
                item.Image = UrlImage;
                itemBUS.update(item);
                return "Upadte place success";
            }
            catch (Exception ex)
            {
                // throw exception
                return ex.Message;
            }
        }

        /// <summary>
        /// Get user places by user device
        /// </summary>
        /// <param name="ItemID"></param>
        /// <returns></returns>
        [WebMethod]
        public String getUserPlaces(string strDeviceItems, string language)
        {
            try
            {
                LanguageBUS lang = new LanguageBUS();
                if (!lang.check(language))
                {
                    language = "vi";
                }
                //remove when support multilang places info
                language = "vi";
                // create new DataTable
                DataTable dtt = new DataTable("Items");
                // create new DataSet
                DataSet ds = new DataSet("GPSTravel");
                DeviceBUS deviceBUS = new DeviceBUS();
                DeviceVO device = deviceBUS.getDevice(strDeviceItems);
                // add DataTable to DataSet
                UserPlacesBUS placeBUS = new UserPlacesBUS();
                dtt = placeBUS.getUserPlaces(device, language);
                if (dtt.Rows[0] != null)
                {
                    dtt.TableName = "Items";
                    ds.Tables.Add(dtt);
                    // Create Xml data from DataSet data
                    string strXdoc = ds.GetXml();
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(strXdoc);
                    // Return Xml Document
                    StringWriter sw = new StringWriter();
                    XmlTextWriter xw = new XmlTextWriter(sw);
                    xdoc.WriteTo(xw);
                    return sw.ToString();
                }
                return "Error: No data";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }

        /// <summary>
        /// upload file
        /// </summary>
        /// <param name="f"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        [WebMethod]
        public string UploadFile(byte[] f, string fileName)
        {
            // the byte array argument contains the content of the file
            // the string argument contains the name and extension
            // of the file passed in the byte array
            try
            {
                // instance a memory stream and pass the
                // byte array to its constructor
                MemoryStream ms = new MemoryStream(f);

                // instance a filestream pointing to the
                // storage folder, use the original file name
                // to name the resulting file
                FileStream fs = new FileStream
                    (System.Web.Hosting.HostingEnvironment.MapPath
                    ("~/Images/") +
                    fileName.Trim(), FileMode.Create);

                // write the memory stream containing the original
                // file as a byte array to the filestream
                ms.WriteTo(fs);

                // clean up
                ms.Close();
                fs.Close();
                fs.Dispose();

                // return OK if we made it this far
                return "OK";
            }
            catch (Exception ex)
            {
                // return the error message if the operation fails
                return ex.Message;
            }
        }
        [WebMethod]
        public string helloWord()
        {
            return "Hello World !";
        }
    }
}
